<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width:100%"
  >
    <el-col>
      <strong v-if="isRoot">
        <i class="el-icon-office-building" /> {{ nodeDept.name }}
      </strong>
      <span v-else>
        {{ nodeDept.name }}
      </span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeDept.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDepartment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editDepartment">编辑子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepartment">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 删除接口
import { delDepartment } from '@/api/departments'
export default {
  props: {
    // 点击获取的
    nodeDept: {
      // 类型
      type: Object,
      // 必填
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 删除请求
    async delDepartment() {
      // 提示用户
      try {
        await this.$confirm('确定要删除吗?')
        // 发送请求 删除
        await delDepartment(this.nodeDept.id)
        // 再次提示删除成功
        this.$message.success('删除成功')
        // 让父组件更新
        this.$emit('delDepartment')
      } catch (error) {
        // console.log(error)
      }
    },
    // 通知父组件点击添加
    addDepartment() {
      this.$emit('addDepartment', this.nodeDept)
    },
    // 通知父组件点击编辑
    editDepartment() {
      this.$emit('editDepartment', this.nodeDept)
      console.log('nodeDept', this.nodeDept)
    }
  }
}
</script>

<style></style>
